<?php

namespace App\Models;

use Egulias\EmailValidator\EmailParser;
use Illuminate\Support\Facades\DB;

class AdminMenusModel
{

    const TOP = -1;
    const MODULE = 0;
    const CHILDMODULE = 1;
    const MENU = 2;
    const BUTTON = 3;

    const MENUHIDE = 1;
    const MENUSHOW = 0;

    public static function getAllStatus()
    {
        return [
            trans('common.show'),
            trans('common.hide')
        ];
    }

    public static function getAllTypes()
    {
        return [
            trans('menu.module'),
            trans('menu.child_module'),
            trans('menu.menu'),
            trans('menu.btn')
        ];
    }

    public static function insertAdminRoles(array $insert = array())
    {
        $time = time();
        if (!isset($insert['create_time'])) {
            $insert['create_time'] = $time;
        }
        if (!isset($insert['update_time'])) {
            $insert['update_time'] = $time;
        }
        return DB::table('admin_menus')->insert($insert);
    }

    public static function getAllAdminMenusForPageMenu(array $params = array())
    {
        $allmenus = array();
        return self::getTree(self::getAllAdminMenus($params), $allmenus);
    }

    public static function getAllAdminMenusForLeftMenu(array $params = array(), $pid = 0)
    {
        return self::getMenusByModule(self::getMenusTreeData(array_map('get_object_vars', self::getAllAdminMenus($params)->toArray()), $pid));
    }

    public static function deleteAdminMenus(array $params = array())
    {
        return self::getAdminMenusObj($params)->delete();
    }

    public static function getAdminMenusExists(array $params = array())
    {
        return self::getAdminMenusObj($params)->exists();
    }

    public static function getAdminMenusData(array $params = array())
    {
        return self::getAdminMenusObj($params)->first();
    }

    public static function updateAdminMenus(array $params = array(), array $update = array())
    {
        return self::getAdminMenusObj($params)->update($update);
    }

    public static function getAllAdminMenus(array $params = array())
    {
        return self::getAdminMenusObj($params)->orderBy('sort', 'desc')->get();
    }

    public static function getAllButtons(string $rule = "", bool $isSuper = false)
    {
        $buttons = array();
        if ($rule == "") {
            return array();
        }
        $menu = self::getAdminMenusData(['rule' => $rule]);
        if (!is_null($menu)) {
            if ($isSuper) {
                $filter = ['type' => self::BUTTON, 'pid' => $menu->id];
            } else {
                $filter = ['type' => self::BUTTON, 'pid' => $menu->id, 'status' => self::MENUSHOW];
            }
            $buttons = self::getAllAdminMenus($filter)->toArray();
        }
        return $buttons;
    }

    public static function getAllChildMenusIds(int $id = 0)
    {
        $curmenu = self::getAdminMenusData(['id' => $id]);
        if (is_null($curmenu)) {
            return [];
        }
        $allMenusIds = array($id);
        $menus = self::getAllAdminMenusForLeftMenu([], $curmenu->id);
        switch ($curmenu->type) {
            case self::MODULE:
                if (!empty($menus)) {
                    foreach ($menus as $childModule) {
                        if (!empty($childModule['child'])) {
                            foreach ($childModule['child'] as $menu) {
                                if (!empty($menu['child'])) {
                                    foreach ($menu['child'] as $btn) {
                                        $allMenusIds = array_merge($allMenusIds, array($btn['id']));
                                    }
                                }
                                $allMenusIds = array_merge($allMenusIds, array($menu['id']));
                            }
                        }
                        $allMenusIds = array_merge($allMenusIds, array($childModule['id']));
                    }
                }
                break;
            case self::CHILDMODULE:
                if (!empty($menus)) {
                    foreach ($menus as $childMenu) {
                        if (!empty($childMenu['child'])) {
                            foreach ($childMenu['child'] as $childBtn) {
                                $allMenusIds = array_merge($allMenusIds, array($childBtn['id']));
                            }
                        }
                        $allMenusIds = array_merge($allMenusIds, array($childMenu['id']));
                    }
                }
                break;
            case self::MENU:
                if (!empty($menus)) {
                    foreach ($menus as $btn) {
                        $allMenusIds = array_merge($allMenusIds, array($btn['id']));
                    }
                }
                break;
            case self::BUTTON:
                $allMenusIds = array($id);
                break;
        }
        return $allMenusIds;
    }


    public static function getMenusTreeData($menus, $pid = 0)
    {
        $arr = [];
        if (empty($menus)) {
            return array();
        }
        foreach ($menus as $key => $v) {
            if ($v['pid'] == $pid) {
                $arr[$key] = $v;
                $arr[$key]['child'] = self::getMenusTreeData($menus, $v['id']);
            }
        }
        return array_values($arr);
    }

    private static function getMenusByModule(array $menus = array())
    {
        if (empty($menus)) {
            return [];
        }
        $module_menus = [];
        foreach ($menus as $menu) {
            $module_menus[$menu['rule']] = $menu;
        }
        return $module_menus;
    }

    private static function getTree($sourceItems, &$targetItems, $pid = 0, $str = '|-')
    {
        $str .= '----';
        foreach ($sourceItems as $k => $v) {
            if ($v->pid == $pid) {
                $v->name = $str . $v->name;
                $targetItems[] = $v;
                self::getTree($sourceItems, $targetItems, $v->id, $str);
            }
        }
        return $targetItems;
    }

    private static function getAdminMenusObj(array $params = array())
    {
        $adminMenus = DB::table('admin_menus');
        if (isset($params['is_not_btn']) && !empty($params['is_not_btn'])) {
            $adminMenus = $adminMenus->where('admin_menus.type', "!=", $params['is_not_btn']);
        }
        if (isset($params['name']) && !empty($params['name'])) {
            $adminMenus = $adminMenus->where('admin_menus.name', "=", $params['name']);
        }
        if (isset($params['rule']) && !empty($params['rule'])) {
            $adminMenus = $adminMenus->where('admin_menus.rule', "=", $params['rule']);
        }
        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $adminMenus = $adminMenus->whereIn('admin_menus.id', $params['id']);
            } else {
                $adminMenus = $adminMenus->where('admin_menus.id', "=", $params['id']);
            }
        }
        if (isset($params['pid'])) {
            $adminMenus = $adminMenus->where('admin_menus.pid', "=", $params['pid']);
        }
        if (isset($params['type'])) {
            $adminMenus = $adminMenus->where('admin_menus.type', "=", $params['type']);
        }
        if (isset($params['status'])) {
            $adminMenus = $adminMenus->where('admin_menus.status', '=', $params['status']);
        }

        return $adminMenus;
    }


}
